package com.suning.sawp.web.controller.member;

import java.security.Principal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.suning.sawp.constants.ClerkConstants;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.member.service.StoreInquiryService;
import com.suning.sawp.member.vo.CateExpandDetailVo;
import com.suning.sawp.member.vo.EmpExpandDetailVo;
import com.suning.sawp.member.vo.StoreExpandDetailVo;
import com.suning.sawp.web.controller.DjBaseController;
import com.suning.sawp.web.exception.MyGsonView;
/**
 * 
 * 门店外拓控制层<br> 
 * 
 * @author 14060886 祝翔龙
 */
@Controller
@RequestMapping("/client/inquiry")
public class StoreInquiryController extends DjBaseController {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);
    
    @Autowired
    private StoreInquiryService storeInquiryService;
    @Autowired
    private StoreManService storeManService;
    
    /**
     * 
     * 功能描述: 查询某门店询价未购活动详情(品类分组)
     *
     * @param dateType
     * @return
     */
    @RequestMapping("/queryInquiryDetailByStore.do")
    public MyGsonView queryInquiryDetailByStore(Principal principal, @RequestParam(defaultValue = "0", required = false) String dateType) {
        LOGGER.info("queryInquiryDetailByStore in, principal={}, dateType={}", principal.getName(), dateType);
        ReturnMsg<StoreExpandDetailVo> retMsg = new ReturnMsg<StoreExpandDetailVo>();
        // 工号
        String staffId = principal.getName();
        // 查询店员信息
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if(null == storeMan){
            // 店员信息异常
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
            return setGsonViewWithEmpty(retMsg);
        }
        StoreExpandDetailVo storeExpandDetail = storeInquiryService.queryInquiryDetailByStore(storeMan.getStoreCode(), dateType);
        retMsg.setData(storeExpandDetail);
        return setGsonViewWithEmpty(retMsg);
    }
    
    /**
     * 
     * 功能描述: 查询某门店询价未购活动详情(品类分组)
     *
     * @param cateCode
     * @param dateType
     * @return
     */
    @RequestMapping("/queryInquiryDetailByCate.do")
    public MyGsonView queryInquiryDetailByCate(Principal principal, String cateCode, 
            @RequestParam(defaultValue = "0", required = false) String dateType) {
        LOGGER.info("queryInquiryDetailByCate in, principal={}, cateCode={}, dateType={}", principal.getName(), cateCode, dateType);
        ReturnMsg<CateExpandDetailVo> retMsg = new ReturnMsg<CateExpandDetailVo>();
        // 工号
        String staffId = principal.getName();
        // 查询店员信息
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if(null == storeMan){
            // 店员信息异常
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
            return setGsonViewWithEmpty(retMsg);
        }
        CateExpandDetailVo cateExpandDetail = storeInquiryService.queryInquiryDetailByCate(storeMan, cateCode, dateType);
        retMsg.setData(cateExpandDetail);
        return setGsonViewWithEmpty(retMsg);
    }
    
    /**
     * 
     * 功能描述: 查询某店员询价未购详情
     *
     * @param employeeId
     * @param dateType
     * @param startDate
     * @param endDate
     * @param startIndex
     * @return
     */
    @RequestMapping("/queryInquiryDetailByEmp.do")
    public MyGsonView queryInquiryDetailByEmp(Principal principal, String employeeId, 
            @RequestParam(defaultValue = "0", required = false) String dateType, String startDate, String endDate, 
            @RequestParam(defaultValue = "0", required = false) String startIndex) {
        LOGGER.info("queryInquiryDetailByEmp in, principal={}, employeeId={}, dateType={}, startDate={}, endDate={}, startIndex={}", 
                principal.getName(), employeeId, dateType, startDate, endDate, startIndex);
        ReturnMsg<EmpExpandDetailVo> retMsg = new ReturnMsg<EmpExpandDetailVo>();
        if (isFieldNull(employeeId)) {
            return getErrorGsonView(retMsg);
        }
        // 工号
        String staffId = principal.getName();
        // 查询店员信息
        StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
        if(null == storeMan){
            // 店员信息异常
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0002);
            return setGsonViewWithEmpty(retMsg);
        }
        String positionId = storeMan.getPositionId();
        EmpExpandDetailVo empExpandDetail = null;
        if (ClerkConstants.STORE_KEEPER.equals(positionId) || ClerkConstants.SALE_GUIDE.equals(positionId)) {
            if (isFieldNull(dateType)) {
                return getErrorGsonView(retMsg);
            }
            empExpandDetail = storeInquiryService.queryInquiryDetailByEmp(storeMan.getStoreCode(), employeeId, dateType, startIndex);
        } else if (ClerkConstants.SALES_CLERK.equals(positionId) || ClerkConstants.SALES_PROMOTION.equals(positionId)) {
            empExpandDetail = storeInquiryService.queryInquiryDetailByEmp4DY(storeMan.getStoreCode(), employeeId, startDate, endDate, startIndex);
        } else {
            LOGGER.warn("未知的岗位：{},试图查询询价未购详情,被拒绝", positionId);
            retMsg.setError(ErrorCodeConstants.ERR_MEM_0012);
            return setGsonViewWithEmpty(retMsg);
        }
        retMsg.setData(empExpandDetail);
        return setGsonViewWithEmpty(retMsg);
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private MyGsonView getErrorGsonView(ReturnMsg retMsg){
        retMsg.setRetFlag(ReturnMsg.FAIL);
        retMsg.setErrorCode(ErrorCodeConstants.ERR_0001);
        retMsg.setErrorMessage(ErrorCodeConstants.ERR_MAP.get(ErrorCodeConstants.ERR_0001));
        return setGsonViewWithEmpty(retMsg);
    }
    
}
